package action;

import global.Constants;
import global.beanUtils.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.NoteCategoryService;
import util.ServletHelp;
import util.StringUtils;
import domain.NoteCategory;

/**
 * 笔记分类Action类.
 * <p>
 * 创建日期：2010-07-01<br>
 * 创建人：Xiyt<br>
 * 修改日期：<br>
 * 修改人：<br>
 * 修改内容：<br>
 * 
 * @author Xiyt
 * @version 1.0
 */
public class NoteCategoryAction extends BaseAction {
	/**
	 * 笔记分类Service
	 * */
	private NoteCategoryService noteCategoryService = null;

	/**
	 * @param noteCategoryService
	 *            the noteCategoryService to set
	 */
	public void setNoteCategoryService(NoteCategoryService noteCategoryService) {
		this.noteCategoryService = noteCategoryService;
	}

	/**
	 * 查询笔记类表
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward query(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		// 参数
		NoteCategory paramNoteCategory = new NoteCategory();
		// 取得笔记分类列表
		List<NoteCategory> noteCategoryList = new ArrayList<NoteCategory>();
		JSONArray datas = new JSONArray();
		noteCategoryList = noteCategoryService.selectByCriteria(paramNoteCategory);
		if(null!=noteCategoryList){
			datas = JSONArray.fromObject(noteCategoryList);
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("datas", datas);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	
	
	/**
	 * 查询笔记分类列表带分页
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward queryForPaging(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		NoteCategory paramNoteCategory = new NoteCategory();
		BeanUtils.populate(paramNoteCategory, map);
		
		// 取得笔记分类列表
		List<NoteCategory> noteCategoryList = new ArrayList<NoteCategory>();
		JSONArray datas = new JSONArray();
		noteCategoryList = noteCategoryService.selectByCriteriaForPaging(paramNoteCategory);
		if(null!=noteCategoryList){
			datas = JSONArray.fromObject(noteCategoryList);
		}
		int count = noteCategoryService.countByCriteria(paramNoteCategory);

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("datas", datas);
		res.put("results", count);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
	

	/**
	 * 获得笔记分类树
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward getNoteCategoryTree(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 返回结果
		JSONArray res = noteCategoryService.selectNoteCategoryForTree();
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 保存笔记分类
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	@SuppressWarnings("unchecked")
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 参数设定
		Map map = request.getParameterMap();
		NoteCategory noteCategory = new NoteCategory();
		BeanUtils.populate(noteCategory, map);
		noteCategory.setStatus(Constants.STATUS_1);
		
		String categoryId = request.getParameter("categoryId");
		if(StringUtils.isNotEmpty(categoryId)){
			// 更新笔记分类
			noteCategoryService.update(noteCategory);
		}else{
			// 添加笔记分类
			noteCategoryService.insert(noteCategory);
		}

		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}

	/**
	 * 删除笔记分类
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 笔记分类ID
		String categoryId = request.getParameter("categoryId");
		// 删除笔记分类
		if (StringUtils.isNotEmpty(categoryId)) {
			// 删除笔记分类
			noteCategoryService.delete(Integer.parseInt(categoryId));
		}
		// 返回结果
		JSONObject res = new JSONObject();
		res.put("success", true);
		ServletHelp.outRequestForJson(request, response, res.toString());
		return null;
	}
}
